package com.mybatis.lo.model;

import com.mybatis.lo.utils.GenericTokenParser;

/**
 * 封装了没有`${}`和动态标签SQL的信息，且初始化后，每次重新获取只需要返回相同sql即可
 * @author zhangbh
 * 2023-06-09 20:09
 **/
public class RawSqlSource implements SqlSource {


    private final BoundSql boundSql;

    public RawSqlSource(SqlNode mixedSqlNode) {
        DynamicContext context = new DynamicContext(null);
        ParameterMappingTokenHandler tokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser parser = new GenericTokenParser("#{", "}", tokenHandler);
        mixedSqlNode.apply(context);
        // 处理sql
        String parse = parser.parse(context.getSql());
        boundSql = new BoundSql(parse, tokenHandler.getParameterMappings());
    }

    @Override
    public BoundSql getBoundSql(Object param) {
        return boundSql;
    }
}
